import 'package:bxultimate/app/theme/app_theme.dart';
import 'package:bxultimate/app/theme/gaps.dart';
import 'package:bxultimate/data/store/config_store.dart';
import 'package:bxultimate/resources/assets.dart';
import 'package:bxultimate/widgets/app/app_gd_map.dart';
import 'package:bxultimate/widgets/app/app_title.dart';
import 'package:bxultimate/widgets/mk_load_image.dart';
import 'package:bxultimate/widgets/mk_page.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';
import 'package:amap_flutter_base/amap_flutter_base.dart';
import 'controller.dart';

class TaskDetailPage extends StatefulWidget {
  const TaskDetailPage({Key? key}) : super(key: key);

  @override
  State<TaskDetailPage> createState() => _TaskDetailPageState();
}

class _TaskDetailPageState extends State<TaskDetailPage> {
  final controller = Get.find<TaskDetailController>();
  final state = Get.find<TaskDetailController>().state;

  @override
  Widget build(BuildContext context) {
    return MkPage(
      child: Scaffold(
        backgroundColor: Colors.transparent,
        appBar: AppBar(
          backgroundColor: Colors.transparent,
          title: Text("任务详情"),
        ),
        body: _buildBody(),
      ),
    );
  }

  _buildBody() {
    return Stack(
      children: [
        Obx(() => state.task.isEmpty == false
            ? AppGdMap(
                point: AppGdMapPoint(
                    icon: ConfigStore.to.baseFileUrl +
                        '/bdjw/intell_jiaotong.png',
                    latLng: LatLng(double.parse(state.task['lat']),
                        double.parse(state.task['lon']))),
              )
            : SizedBox()),
        Positioned(bottom: 0, child: _buildTaskDetail())
      ],
    );
  }

  _buildTaskDetail() {
    return Obx(() => Container(
          width: Get.width,
          decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.only(
              topLeft: Radius.circular(12),
              topRight: Radius.circular(12),
            ),
          ),
          padding: EdgeInsets.symmetric(
              horizontal: AppTheme.iDefaultPadding, vertical: 20),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Obx(() => Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      AppTitle(
                        title: state.task['isAccept'] == "-1" ? '待接受' : '待完成',
                        icon: Assets.taskLocation,
                      ),
                      GestureDetector(
                        onTap: controller.toNavigator,
                        child: AppTitle(
                          title: '导航',
                          icon: Assets.taskLocation,
                        ),
                      ),
                    ],
                  )),
              Gaps.vGap16,
              Text(
                "${state.task['title']}",
                style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
              ),
              Gaps.vGap8,
              Row(
                children: [
                  Text(
                    "${state.task['createTime']}",
                    overflow: TextOverflow.ellipsis,
                    style: TextStyle(fontSize: 12),
                  ),
                  Gaps.hGap8,
                  TDTag(
                    "${state.task['taskTypeStr']}",
                    theme: TDTagTheme.primary,
                    size: TDTagSize.small,
                  ),
                  Gaps.hGap8,
                  TDTag("${state.task['distance']}km",
                      backgroundColor: Color(0xFFBDD2FD),
                      textColor: Color(0xFF5B8FF9),
                      size: TDTagSize.small),
                ],
              ),
              Gaps.vGap8,
              Text(
                "${state.task['info']}",
                overflow: TextOverflow.ellipsis,
                maxLines: 2,
                style: TextStyle(fontSize: 12),
              ),
              Gaps.vGap8,
              AppTitle(
                title: "${state.task['address']}",
                textStyle: TextStyle(fontSize: 12, color: Color(0xff606266)),
                icon: Assets.taskLocation,
              ),
              Gaps.vGap16,
              Row(
                children: [
                  state.task['isAccept'] == "-1"
                      ? Expanded(
                          child: TDButton(
                            onTap: () {
                              controller.updateTask(TaskDetailStatus.REFUSE);
                            },
                            text: '拒绝',
                            theme: TDButtonTheme.danger,
                            shape: TDButtonShape.circle,
                            height: 42,
                            iconWidget: MkLoadImage(
                              Assets.taskOk,
                              width: 24,
                              height: 24,
                            ),
                          ),
                        )
                      : SizedBox(),
                  Gaps.hGap8,
                  Expanded(
                    child: TDButton(
                      onTap: () {
                        state.task['isAccept'] == "-1"
                            ? controller.updateTask(TaskDetailStatus.ACCEPT)
                            : controller.updateTask(TaskDetailStatus.COMPLETE);
                      },
                      text: state.task['isAccept'] == "-1" ? '去接受' : '已完成',
                      theme: TDButtonTheme.primary,
                      shape: TDButtonShape.circle,
                      height: 42,
                      iconWidget: MkLoadImage(
                        Assets.taskOk,
                        width: 24,
                        height: 24,
                      ),
                    ),
                  )
                ],
              )
            ],
          ),
        ));
  }

  @override
  void dispose() {
    Get.delete<TaskDetailController>();
    super.dispose();
  }
}
